package com.lhf.fvsgenerator.config;

import com.lhf.fvsgenerator.entity.Column;
import com.lhf.fvsgenerator.entity.Table;

import java.util.List;

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/10/24 10:57
 */
public class Generator {

    public static final String MODEL_SYSTEM = "com.lhf.fvsgenerator";
    public static final String MODEL_MAIN = "com.lhf.fvsgenerator";

    private final FreeMarkerConfig config;
    private DataModel model;

    public Generator(FreeMarkerConfig config) {
        this.config = config;
    }


    private void init(Table table) {
        model = new DataModel();
        model.setColumns(this.column(table.getColumns()));
        model.setDao("dao");
        model.setEntity("entity");
        model.setController("controller");
        model.setService("service");
        model.setServiceImpl("service.impl");
        model.setSysModel(MODEL_SYSTEM);
        model.setMainModel(MODEL_MAIN);
        model.setClassName(this.camelCase(table.getTableName(), 1));
    }

    public List<Column> column(List<Column> columns) {
        for (Column column : columns) {
            column.setColumnName(this.camelCase(column.getColumnName(), 0));
        }
        return columns;
    }

    public String camelCase(String str, int type) {
        StringBuilder sb = new StringBuilder();
        String[] s = str.split("_");
        String s1 = s[0];
        if (type == 1) {
            //类
            sb.append(s1.substring(0, 1).toUpperCase()).append(s1.substring(1));
        } else {
            //属性
            sb.append(s1.substring(0, 1).toLowerCase()).append(s1.substring(1));
        }
        for (int i = 1; i < s.length; i++) {
            sb.append(s[i].substring(0, 1).toUpperCase()).append(s[i].substring(1));
        }
        return sb.toString();
    }


    /**
     * 代码生成
     *
     * @param table
     */
    public void generator(Table table) {
        this.init(table);
    }
}
